package leetcode;

import java.util.Stack;

/**
 * 罗马数字转整数
 * 13th 简单
 * https://leetcode-cn.com/problems/roman-to-integer/description/
 */
public class romanToInt {

    public static void main(String[] args) {

    }

    private int romanToInt(String s){
        int res = 0;
        for(int i = 0; i < s.length(); i++){
            char cur = s.charAt(i);
            if(cur == 'I'){
                int nextIndex = i + 1;
                if(nextIndex < s.length()){
                    char next = s.charAt(nextIndex);
                    if(next == 'V'){
                        res += 4;
                        i++;
                    }else if(next == 'X'){
                        res += 9;
                        i++;
                    }else{
                        res += 1;
                    }
                }else{
                    res += 1;
                }
            }else if(cur == 'X'){
                int nextIndex = i + 1;
                if(nextIndex < s.length()){
                    char next = s.charAt(nextIndex);
                    if(next == 'L'){
                        res += 40;
                        i++;
                    }else if(next == 'C'){
                        res += 90;
                        i++;
                    }else{
                        res += 10;
                    }
                }else{
                    res += 10;
                }
            }else if(cur == 'C'){
                int nextIndex = i + 1;
                if(nextIndex < s.length()){
                    char next = s.charAt(nextIndex);
                    if(next == 'D'){
                        res += 400;
                        i++;
                    }else if(next == 'M'){
                        res += 900;
                        i++;
                    }else{
                        res += 100;
                    }
                }else{
                    res += 100;
                }
            }else if(cur == 'V'){
                res += 5;
            }else if(cur == 'L'){
                res += 50;
            }else if(cur == 'D'){
                res += 500;
            }else if(cur == 'M'){
                res += 1000;
            }
        }
        return res;
    }

}
